// -= code_dragon/index coding by Scott in 2018-12-13-15:15 with IDE:PyCharm =-
var vm = new Vue({
    el: '#app',
    data: {
        host: host,
        static_host,
        token: sessionStorage.token || localStorage.token,
        username: sessionStorage.username || localStorage.username,
        user_id: sessionStorage.user_id || localStorage.user_id,
        results: [],  // 新闻数据
        next_page: '',  // 下一页链接
        previous_page: '',  // 上一页链接
        category_list: [],
        count: 0, // 总条目数
        current: '',
        author: [],
        category: [],  // 新闻的分类
        avatar: '',
    },
    mounted: function () {
        if (this.get_url() !== 'index.html') {
            location.href = 'index.html'
        }
        // 判断用户的登录状态
        if (this.user_id && this.token) {
            axios.get(this.host + '/user/', {
                // 向后端传递JWT token的方法
                headers: {
                    'Authorization': 'JWT ' + this.token
                },
                responseType: 'json',
            })
                .then(response => {
                    // 加载用户数据
                    this.user_id = response.data.id;
                    this.username = response.data.username;
                    this.avatar = response.data.avatar;
                    if (!this.avatar) {
                        this.avatar = './img/widget-photo.png'
                    }
                })
                .catch(error => {
                    if (error.response.status == 401 || error.response.status == 403) {
                        location.href = '/person-loginsign.html?next=/index.html';
                    }
                });
        } else {
            this.user_id = null;
            this.username = null;
            this.token = null
        }
        this.get_index();
        axios.get(this.host + '/cats/', {
            responseType: 'json',
            withCredentials: true,
        }).then(response => {
            this.category_list = response.data;
        }).catch(error => {
            console.log(error.response.data);
        });
    },
    methods: {
        hover: function (args) {
            // alert(args.target().children('.hoverinfo').css('style','block'))
            // args.target().children('.hoverinfo').css('style','block')
            // style = "display: block"
        },
        // 获取url路径
        get_url: function () {
            var url = document.location.toString();
            var arrUrl = url.split('/');
            if (!arrUrl[3]) {
                return 'index.html'
            }
            return arrUrl[3]
        },
        isLogined: function (tag) {
            if (this.user_id && this.token) {

            } else {
                location.href = '/person-loginsign.html';
            }
        },
        addClass: function (index) {
            this.current = index;
        },
        get_news: function (id) {
            if (id === 1) {
                this.get_index()
            } else {
                axios.get(this.host + '/cats/' + id, {
                    responseType: 'json',
                    withCredentials: true,
                }).then(response => {
                    this.results = response.data.results;
                    this.next_page = response.data.next;
                    this.previous_page = response.data.previous;
                    this.count = response.data.count;
                }).catch(error => {
                    console.log(error.response.data);
                });
            }
        },
        get_index: function () {
            axios.get(this.host + '/news/', {
                responseType: 'json',
                withCredentials: true,
            }).then(response => {
                this.results = response.data.results;
                this.next_page = response.data.next;
                this.previous_page = response.data.previous;
                this.count = response.data.count;
            })
                .catch(error => {
                    console.log(error.response.data);
                });
        }
    }
});